package com.changgou.user.util;

/**
 * @Project: changgou-parent
 * @Package: entity
 * @Time: 2019/8/19 16:28 星期一
 * @User: Super Note
 **/

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/***
 * 短信发送工具类
 */
public class SMSUtils {
    public static final String VALIDATE_CODE = "SMS_172604359";//模板编号

    /***
     * 发送短信
     * @param phoneNumbers
     * @param param
     * @throws ClientException
     */
    public static void sendShortMessage(String phoneNumbers, String param) throws ClientException {
        // 设置超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        final String product = "Dysmsapi";// 短信API产品名称（固定，无需修改）
        final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（固定，无需修改）
        //AK
        final String accessKeyId = "LTAIFdV1yWqzeKg1";//    accessKeyId
        final String accessKeySecret = "K9XEyFgFJT7XSIhnmSjvrpsTR0StKI";//  accessKeySecret

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);

        IAcsClient acsClient = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName("畅购");
        request.setTemplateCode(VALIDATE_CODE);
        request.setTemplateParam("{\"code\":\"" + param + "\"}");

        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            // 请求成功
            System.out.println("请求成功");
        }
    }
}
